﻿<script type="text/javascript">
    var m_BalanceList;
    function initPage() {
        var urlstr = '@Url.Content("~/IM/AllDictionary/GetMaterialTypeForBalanceList")';
        $.EUIcomboboxTree("#MType", {
            url: urlstr,//ajax后台取数据路径，返回的是json格式的数据
            panelHeight: 350,
            OneOption: [{
                id: "",
                text: "--请选择--",
            }],
            onSelect: function (rec) {
                $.ajax({
                    type: 'POST',
                    url: '@Url.Content("~/IM/I_Balance/GetMaxReportTime")',
                    data:
                        {
                            MTypeID: rec.id
                        },
                    dataType: "json",
                    success: function (jsData) {
                        if (jsData.success) {
                            $('#ReportMonth').textbox('setText', jsData.reportMonth);
                            $('#ReportMonth').textbox({ 'disabled': true });
                        } else {
                            $.showMsg("该物资种类上月未产生数据", "系统提示");
                            $('#ReportMonth').textbox('setText',"" );
                            $('#ReportMonth').textbox({ 'disabled': false });
                        }
                    }
                });
            }
        });
        $("#gridBalanceDetailList").datagrid({
            width: 'auto',
            height: 'auto',
            collapsible: true,
            fitColumns: false,
            striped: true,         
            singleSelect: true,         
            loadMsg: '数据加载中请稍后……',          
            pagination: false,//启用分页，默认每页10行
            rownumbers: true,//显示页码，默认 提供 10 - 50 的页容量选择下拉框                   
            columns: [
                [            
               //{ title: '月份', field: 'ReportMonth', width: '60', sortable: false, align: "center" },
               //{ title: '代码', field: 'MCode', width: '50', sortable: false, align: "center" },
               { title: '名称', field: 'MName', width: '12%', sortable: false, align: "center" },
               { title: "月初数量", field: "BeginningCounts", width: '5%', align: "center" },
               { title: "月初总价", field: "BeginningPrice", width: '6%', align: "center" },
               { title: "入库数量", field: "IncomeCounts", width: '5%', align: "center" },
               { title: "入库总价", field: "IncomePrice", width: '6%', align: "center" },
               { title: "出库数量", field: "PayCounts", width: '5%', align: "center" },
               { title: "出库总价", field: "PayPrice", width: '6%', align: "center" },
               { title: "月末数量", field: "SurplusCounts", width: '5%', align: "center" },
               { title: "月末总价", field: "SurplusPrice", width: '6%', align: "center" },
               { title: '统计时间', field: 'SurplusTime', width: '12%', align: 'center', sortable: false, formatter: $.formatTime },
               {
                   title: "财务修改价", field: "UpdataPrice", width: '6%', align: "right",
                   formatter: function (value, rec, rowIndex) {
                       return '  <input class="UpdataPricecls" id=\"UpdataPrice' + rowIndex + '\" name = \"UpdataPriceText\" value=\'' + value + '\'/>';
                   }
               },
                {
                    field: 'opt', title: '', width: '7%', align: 'center', frozen: 'true',
                    formatter: function (value, rec, rowIndex) {
                        return '<a  class="quedingcls" href="javascript:void(0)"  onclick="clickRow(' + rec.MaterialID + ',' + rowIndex + ')">确定</a>';
                    }
                },
               { title: 'MaterialID', field: 'MaterialID', sortable: false, hidden: true }
                ]
            ],
            onLoadSuccess: function (data) {
                $('.quedingcls').linkbutton({
                    text: '确定',
                    plain: true,
                    iconCls: 'icon-custom-cChange'
                })
                $('.UpdataPricecls').numberbox({
                    width: 60,
                    precision: 2
                });
            }
        });
    }

    function clickRow(id, rid) {
        var upinfo = $('#UpdataPrice' + rid).numberbox('getValue');
        for (var i = 0; i < m_BalanceList.length; i++) {
            if (m_BalanceList[i].MaterialID == id) {
                m_BalanceList[i].SurplusPrice = parseFloat(m_BalanceList[i].SurplusPrice) + parseFloat(upinfo);
                m_BalanceList[i].UpdataPrice = upinfo;
            }
        }
        $('#gridBalanceDetailList').datagrid('loadData', m_BalanceList);

    }
    function SelectReport() {
        $.ajax({
            type: 'POST',
            url: '@Url.Content("~/IM/I_Balance/SelectBalanceReport")',
            data:
                {
                    MType: $('#MType').combobox('getValue'),                
                    ReportMonth: $('#ReportMonth').textbox('getText')
                },
            dataType: "json",
            success: function (jsData) {
                if (jsData.success) {
                    m_BalanceList = jsData.list;
                    $("#gridBalanceDetailList").datagrid('loadData', jsData.list);
                } else {
                    $("#gridBalanceDetailList").datagrid('reload');
                    $.showMsg(jsData.msg, "系统错误提示！", function () {
                        //$('#BalanceDetailList').dialog('close');

                    });
                }
            }
        });
    }

    function SubmitReport() {
        //$('#btnSelect').linkbutton('disable');
        $('#btnSubmit').linkbutton('disable');
        var addInfo = m_BalanceList;
        for (var i = 0; i < addInfo.length; i++) {
            addInfo[i].SurplusTime = $.formatTime(addInfo[i].SurplusTime);
        }
        $.ajax({
            type: 'POST',
            url: '@Url.Content("~/IM/I_Balance/SubmitReport")',
            data:
                {
                    ReportStr: JSON.stringify(addInfo),
                    MType: $('#MType').combobox('getValue'),                 
                    ReportMonth: $('#ReportMonth').textbox('getText')        
                },
            dataType: "json",
            success: function (jsData) {
                $.handleJsonResult(jsData, function () {
                    m_BalanceList.length = 0;
                    $('#btnSubmit').linkbutton('enable');
                    $('#balancegrid').datagrid("reload");
                    $('#BalanceDetailList').dialog("close");                 
                }, function () {
                    //$('#btnSelect').linkbutton('enable');
                    //$('#btnSubmit').linkbutton('enable');
                });
            }
        });
    }
</script>


<div id="BalanceDetailList" class="easyui-dialog" title="库存出入信息财务清单" data-options="modal:true,collapsible:false,minimizable:false,maximizable:false,closed:true,resizable:false,iconCls:'icon-custom-cPrice'" style="width:1000px;height:500px;padding:0px;">
    <div data-options="region:'center'" style="overflow: hidden; padding: 10px;width:900px;">
        <table>
            <tr>
                <td width="80px" style="vertical-align:text-top;text-align:right">物资类型：</td>
                <td width="150px" style="vertical-align:text-top">
                    <select id="MType" class="easyui-combobox" style="width: 150px"></select>
                </td>              
                <td width="100px" style="vertical-align:text-top;text-align:right">报表月份：</td>
                <td width="130px" >
                    <input id="ReportMonth" class="easyui-textbox" data-options="prompt:'例：202101'" />
                    @*<select id="ReportMonth" class="easyui-textbox" style="width: 130px">*@
                        @*<option value="2016">2016</option>
                        <option value="2017">2017</option>
                        <option value="2018">2018</option>
                        <option value="2019">2019</option>
                        <option value="2020">2020</option>
                        <option value="2021">2021</option>
                        <option value="2022">2022</option>
                        <option value="2023">2023</option>
                        <option value="2024">2024</option>
                        <option value="2025">2025</option>*@                     
                    @*</select>*@
                </td>
                @*<td width="40px" style="vertical-align:text-top;text-align:right">月：</td>
                <td width="130px" style="vertical-align:text-top">
                    <select id="ReportMonth" class="easyui-combobox" style="width: 130px">
                        <option value="01">01</option>
                        <option value="02">02</option>
                        <option value="03">03</option>
                        <option value="04">04</option>
                        <option value="05">05</option>
                        <option value="06">06</option>
                        <option value="07">07</option>
                        <option value="08">08</option>
                        <option value="09">09</option>
                        <option value="10">10</option>
                        <option value="11">11</option>
                        <option value="12">12</option> 
                    </select>
                </td>*@
                <td width="100px" style="vertical-align: text-top; text-align: right">
                    <a href="javascript:void(0)" id="btnSelect" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="SelectReport()">查询数据</a>
                </td>
                <td width="200px" style="vertical-align: text-top; text-align: right">
                    <a href="javascript:void(0)" id="btnSubmit" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="SubmitReport()">生成报表</a>
                </td>
            </tr>
        </table>
    </div>
    <div data-options="region:'north'" style="background: #fafafa; overflow: hidden;">
        <table id="gridBalanceDetailList"></table>
    </div>
</div>